using System;
using System.IO;
using System.IO.Compression;

namespace Wantn.Share
{
	public sealed class DataCmp
	{
		public static byte[] Compress(byte[] binIn)
		{
			byte[] array = null;
			MemoryStream memoryStream = null;
			GZipStream gZipStream = null;
			try
			{
				memoryStream = new MemoryStream();
				gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, leaveOpen: true);
				gZipStream.Write(binIn, 0, binIn.Length);
				gZipStream.Close();
				gZipStream.Dispose();
				memoryStream.Seek(0L, SeekOrigin.Begin);
				return memoryStream.GetBuffer();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (gZipStream != null)
				{
					gZipStream.Close();
					gZipStream.Dispose();
				}
				if (memoryStream != null)
				{
					memoryStream.Close();
					memoryStream.Dispose();
				}
			}
		}

		public static byte[] Decompress(byte[] binIn)
		{
			byte[] array = null;
			MemoryStream memoryStream = null;
			GZipStream gZipStream = null;
			MemoryStream memoryStream2 = null;
			try
			{
				memoryStream = new MemoryStream();
				memoryStream.Write(binIn, 0, binIn.Length);
				memoryStream.Position = 0L;
				gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress, leaveOpen: true);
				memoryStream2 = new MemoryStream();
				int num = 0;
				byte[] array2 = new byte[1024];
				while ((num = gZipStream.Read(array2, 0, array2.Length)) > 0)
				{
					memoryStream2.Write(array2, 0, num);
				}
				gZipStream.Close();
				gZipStream.Dispose();
				memoryStream.Close();
				memoryStream.Dispose();
				memoryStream2.Seek(0L, SeekOrigin.Begin);
				return memoryStream2.GetBuffer();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (gZipStream != null)
				{
					gZipStream.Close();
					gZipStream.Dispose();
				}
				if (memoryStream != null)
				{
					memoryStream.Close();
					memoryStream.Dispose();
				}
				if (memoryStream2 != null)
				{
					memoryStream2.Close();
					memoryStream2.Dispose();
				}
			}
		}

	}
}
